<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/13
 * Time: 上午11:50
 */

namespace app\src\insurance\logic;


use app\src\base\logic\BaseLogic;
use app\src\insurance\model\Insurance;
use app\src\insurance\model\InsuranceApply;
use app\src\extend\Page;
use app\src\powersystem\logic\AuthGroupAccessLogic;
use app\src\seller\helper\OrderCodeHelper;
use app\src\seller\emnu\SellerOrderStatusEmnu;
use app\src\powersystem\enum\AdminRoleEnum;

class InsuranceApplyLogic extends BaseLogic{
    public function _init()
    {
        $this->setModel(new InsuranceApply());
    }

    /*
    * 添加车险申请
    */
    public function addApply($entity){
        //每种保险一次只能申请一次
        $map = ['insurance_id' => $entity['insurance_id'], 'uid' => $entity['uid'], 'apply_status' => '0'];
        $result = $this->count($map);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if($result['info'] > 0) return $this->apiReturnErr('已经有正在处理的申请，请耐心等待');
        $data = [
            'uid' => $entity['uid'],
            'insurance_id' => $entity['insurance_id'],
            'apply_info' => $entity['apply_info'],
            'create_time'=>time(),
            'update_time'=>time(),
            'handle_uid'=>0,
            'apply_status' => 0,
            'status' => 0
        ];
        $result = $this->add($data);
        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * @return array
     */
    public function ApplyList($map,$page = array('curpage'=>1,'size'=>10),$order){
        $model=$this->getModel();

        $query=$model->alias('ia')
            ->field('ia.*,m.nickname,m.head')
            ->join(['common_member'=>'m'],'m.uid=ia.uid','LEFT');

        $map['ia.status']=0;
        if(!empty($map))$query=$query->where($map);
        if(!empty($order))$query=$query->order($order);

        $start = max(intval($page['curpage'])-1,0) * intval($page['size']);
        $list = $query -> limit($start,$page['size']) -> select();
        $query = $model->alias('ia')
            ->field('ia.*,m.nickname,m.head')
            ->join(['common_member'=>'m'],'m.uid=ia.uid','LEFT');
        if(!empty($map))$query=$query->where($map);
        $count = $query->count();
        $data = [];
        foreach ($list as $vo){
            if(method_exists($vo,"toArray")){
                array_push($data,$vo->toArray());
            }else{
                array_push($data,$vo);
            }
        }
        $result=["count" => $count, "list" => $data];


        return $this->apiReturnSuc($result);

    }





    /*
     * 删除或恢复申请
     * status,-1删除，0恢复
     */
    public function deleteApply($admin_uid,$id,$status){
        //判断权限
        $this->checkPower($admin_uid);
        $entity=[
            'status'=>$status,
        ];
        $result= (new InsuranceApplyLogic())->saveByID($id,$entity);
        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /*
     * 判断后台用户是否有此权限
     */
    public function checkPower($uid){
        $map['uid']=$uid;
        $access_ids=$this->getAdminauthaccess();

        $map['group_id']=['in',$access_ids];
        $access=(new AuthGroupAccessLogic())->getInfo($map);
        if(!empty($access['info'])){
            return true;
        }else{
            $this->error('您操作的用户不存在');
        }
    }

    /*
     * 获取管理员身份的access序号
     */
    public function getAdminauthaccess(){
        $ids=[];
        $role_1=AdminRoleEnum::getRoleIDBy(AdminRoleEnum::ROLE_Admin);
        $role_2=AdminRoleEnum::getRoleIDBy(AdminRoleEnum::ROLE_Option);
        $role_3=AdminRoleEnum::getRoleIDBy(AdminRoleEnum::ROLE_Service);
        $ids[]=AdminRoleEnum::getRoleIDBy($role_1);
        $ids[]=AdminRoleEnum::getRoleIDBy($role_2);
        $ids[]=AdminRoleEnum::getRoleIDBy($role_3);
        return $ids;
    }


    /*
     * 用户获取保险申请订单
     */
    public function queryApplys($map,$order,$field){
        $field='ia.id,ia.insurance_id,ia.uid,ia.apply_info,ia.apply_status,ia.update_time,i.name,i.desc,ioi.apply_id,ioi.pay_img,ioi.pay_info,ioi.order_status';
        $query=$this->getModel()->alias('ia');
        $query=$query->order($order);

        $list=$query
            ->join(['itboye_insurance'=>'i'],'i.id=ia.insurance_id','left')
            ->join(['itboye_insurance_order_info'=>'ioi'],'ioi.apply_id=ia.id','left')
            ->field($field)
            ->where($map)
            ->select();

        $data = [];
        foreach ($list as $vo){
            if(method_exists($vo,"toArray")){
                array_push($data,$vo->toArray());
            }
        }
        return $this->apiReturnSuc($data);

    }


}